Farmer John
arranged n of his cows, each of which belongs to one of two
breeds: Holsteins or Guernseys. He recorded this
arrangement as a string of characters, each of which is either H or
G, respectively. Unfortunately, when the cows arrived at the farm and John
lined them up again, the resulting string turned out to be different from the
original one.
Let us denote these
two strings by a and b, where a is the original string that Farmer John wanted to see,
and b is the
string obtained after the cows arrived. Farmer John asked his cousin Ben for
help.
After several
months of work, Ben created an amazing machine called MCBF-3000, which can
select any substring and replace all G characters in it with H, and all H characters
with G. Now Farmer John wants to know the minimum number of applications of
this machine required to transform string b into string a. Help
Farmer John solve this problem.
Input. The first line
contains an integer n (1 ≤ n ≤ 1000) – the
number of cows. The following two lines contain the strings a and b.
Each string consists only of the characters H and G.
Output. Print the minimum
number of applications of the MCBF-3000 machine required to transform string
b into string a.
|
Sample input |
Sample output |
|
7 GHHHGHH HHGGGHH |
2 |
greedy

Each such interval
corresponds to exactly one application of the MCBF-3000 machine, since the
machine inverts all characters in the selected substring, thereby fixing all
mismatches within the interval simultaneously. Therefore, the answer to the
problem is the number of such maximal intervals.
Example
For the given example,
there are two non-overlapping intervals on which the MCBF-3000 machine must be
applied.

cin >> n >> a >> b;
Store the length of
the current mismatch interval in the variable cnt. In the variable res
we accumulate the answer – the number of times the machine is applied.
res = cnt = 0;
Process the
characters of the strings sequentially.
for (i = 0; i < n; i++)
If ai
≠ bi, increase cnt by
one.
if (a[i] != b[i])
cnt++;
Otherwise (if ai
= bi), the current mismatch interval ends: we reset cnt
to zero and increment the interval counter res by one.
else
{
if (cnt > 0) res++;
cnt = 0;
}
If cnt >
0, this means that there is an unfinished mismatch interval at the end of the
string, which also requires one application of the machine.
if (cnt > 0) res++;
Print the answer.
cout << res << endl;
Read the input data.
n = int(input())
a = input()
b = input()
Store the length of the current mismatch interval in the variable cnt.
In the variable res we accumulate the answer – the number of times the
machine is applied.
res = cnt = 0
Process the characters of the strings sequentially.
for i in range(n):
If ai ≠ bi, increase
cnt by one.
if a[i] != b[i]: cnt += 1
Otherwise (if ai = bi), the current
mismatch interval ends: we reset cnt to zero
and increment the interval counter res by one.
else:
if cnt > 0: res += 1
cnt = 0
If cnt > 0, this means that there is an unfinished mismatch
interval at the end of the string, which also requires one application of the
machine.
if cnt > 0: res += 1
Print the answer.
print(res)